package com.lxf.education.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lxf.education.pojo.StudentPojo;
import com.lxf.education.pojo.TeacherPojo;
import com.lxf.education.service.StudentListService;
import com.ramostear.captcha.HappyCaptcha;
import com.ramostear.captcha.support.CaptchaType;

import io.swagger.annotations.ApiOperation;

/**
 * @学生登录
* @author:LuoXingFa;
* @version:Date:2022年5月6日上午11:56:23
*/
@Controller
@RequestMapping("/stu")
public class StudentLoginController {

	//映射出service接口
	
			@Resource
			StudentListService studentListService;
			/**
			 * 验证码生成
			 * 
			 * @param request
			 * @param response
			 */
			@RequestMapping("/getVerifyCode")
			@ApiOperation(value = "登录模块生成的验证码")
			public void getVerificationCode(HttpServletRequest request, HttpServletResponse response) {
				HappyCaptcha.require(request, response).type(CaptchaType.WORD_NUMBER_LOWER).build().finish();
				HappyCaptcha.require(request, response).width(100).height(40);
			}
			
			/**
			 * 
			 * @param code 验证码
			 * @param request 
			 * @param response
			 * @param model
			 * @return
			 */
		     //登录成功跳转到首页
		     @RequestMapping("/index")
		     public String login(String code,HttpServletRequest request, HttpServletResponse response, Model model) {
		         try {
		             String username = request.getParameter("username");
		             String password = request.getParameter("password");
		             String remeber = request.getParameter("remember");

		             List<TeacherPojo> listTeacher = studentListService.findStu(username, password);
					//记住密码的操作
					 if (remeber!=null) {
						 Cookie cookieUser = new Cookie("username", username);
						 Cookie cookiePwd = new Cookie("password", password);
						 //设置最大缓存时间为一周
						 cookieUser.setMaxAge(60*60*24*7);
						 cookiePwd.setMaxAge(60*60*24*7);
						 //加入cookie缓存
						 response.addCookie(cookieUser);
						 response.addCookie(cookiePwd);
					 }

					 boolean flag = HappyCaptcha.verification(request,code,true);//验证码校验
				         if(listTeacher.size()>0){
				         	    //加入视图中显示在前端
				        	    model.addAttribute("user", username);
//				                model.addAttribute("student", listStudent);
//				                model.addAttribute("teacher", listTeacher);
				                //把登录用户加入到session缓存中
				        	    request.getSession().setAttribute("user", username);
				             if(flag){
				                
					                //返回到主界面
					                return "index";
					             }else {
					 	      		model.addAttribute("msg","验证码错误或不能为空");
					 				return "login";
					 		    } 
				             
				             }else {
								model.addAttribute("msg", "用户名或密码错误!");
								HappyCaptcha.remove(request);//清除缓存，防止验证码二次使用
				            	return "login";
							}
				        
		         } catch (Exception e) {
		             e.printStackTrace();
		         }
		         return null;
		     }

			/**
			 * @查看个人资料
			 * @param request
			 * @return
			 */
			@RequestMapping("/profile")
		     public String getProfile(HttpServletRequest request,Model model){
		     	HttpSession session = request.getSession();
		     	//获取session中的对象
				 String name = (String)session.getAttribute("user");
				 StudentPojo studentPojo = studentListService.findStuByName(name);
				 if (studentPojo!=null) {
					 return "profile";
				}
				model.addAttribute("unlogin", "您还未登录！");
		     	return "index";
			 }

			/**
			 * 退出登录操作
			 * @param request
			 * @return
			 */
			@RequestMapping("/loginout")
		     public String loginout(HttpServletRequest request){

				 HttpSession session = request.getSession();
				 session.removeAttribute("user");
				 //退出登录并重定向到登录界面
		     	return "redirect:/login";
			 }
}
